﻿Public Class oPromotionCustomerExclusive
    Public Function UpdateJsonPromotionCustomerEx(ByVal lineID As String, ByVal promotionID As String, _
                                   ByVal sourceType As String, ByVal sourceValue As String, ByVal databaseName As String, ByVal isInsert As Boolean) As String
        Dim connect As New Connection()
        Try
            Dim errMsg As String = connect.setSQLDB(databaseName)
            If errMsg.Length = 0 Then
                Dim params = New Object() {promotionID, sourceType, sourceValue}
                Dim query As New StringBuilder
                If isInsert = True Then
                    query.Append(" INSERT INTO [oPromotionCustomerExclusive]")
                    query.Append("  ([PromotionID]")
                    query.Append(" ,[SourceType]")
                    query.Append(" ,[SourceValue])")
                    query.Append(" VALUES (@Param1,@Param2,@Param3)")
                    Dim count As Integer = connect.Object_Execute_SAP(query.ToString(), params)
                Else
                    query.Append("UPDATE  [oPromotionCustomerExclusive] SET ")
                    query.Append("  [SourceType] = @Param1, [SourceValue] = @Param2")
                    query.Append(" WHERE [LineID] = @Param3")
                    connect.Object_Execute_SAP(query.ToString(), New Object() {sourceType, sourceValue, lineID})
                End If
            Else
                Return errMsg
            End If
        Catch ex As Exception

            Return ex.Message
        End Try
        Return String.Empty
    End Function
    Public Function DeleteJsonPromotionCustomerEx(ByVal dataBaseName As String, ByVal lineID As String) As String
        Dim connect As New Connection()
        Try
            Dim errMsg As String = connect.setSQLDB(dataBaseName)
            If errMsg.Length = 0 Then
                Dim params = New Object() {lineID}
                Dim query As New StringBuilder
                query.Append(" DELETE FROM [oPromotionCustomerExclusive]")
                query.Append("  WHERE LineID =  @Param1")
                Dim count As Integer = connect.Object_Execute_SAP(query.ToString(), params)
            Else
                Return errMsg
            End If
        Catch ex As Exception

            Return ex.Message
        End Try
        Return String.Empty
    End Function
    Public Function GetAllPromotionCustomerExByPromotionID(ByVal dataBaseName As String, ByVal promotionID As String) As DataSet
        Dim connect As New Connection()
        Try
            Dim ds As New DataSet("oPromotionCustomerExclusive")
            Dim errMsg As String = connect.setSQLDB(dataBaseName)
            Dim query As New StringBuilder
            If errMsg.Length = 0 Then
                query.Append(" 	SELECT * FROM (")
                query.Append(" (SELECT T0.*,T1.*,T2.CustomerName AS SourceValueName ")
                query.Append(" FROM oPromotionCustomerExclusive T0 JOIN oReferenceValue T1 ON T0.SourceType = T1.ReferenceValue ")
                query.Append(" JOIN oCustomer T2 ON T2.CustomerID = T0.SourceValue")
                query.Append(" WHERE T1.ReferenceCode = 'CustomerSourceType' AND T0.PromotionID  =  @Param1")
                query.Append(" AND T1.ReferenceName ='Customer Code')")
                query.Append(" UNION")
                query.Append(" (SELECT T0.*,T1.*,T2.PropertyDetailName AS SourceValueName ")
                query.Append(" FROM oPromotionCustomerExclusive T0 JOIN oReferenceValue T1 ON T0.SourceType = T1.ReferenceValue ")
                query.Append(" JOIN oCustomerPropertyDetail T2 ON T2.PropertyDetailID = T0.SourceValue")
                query.Append(" WHERE T1.ReferenceCode = 'CustomerSourceType' AND T0.PromotionID  =  @Param1")
                query.Append(" AND T1.ReferenceName ='Customer Property' )")
                query.Append(" UNION")
                query.Append(" (SELECT T0.*,T1.*,SourceValue AS SourceValueName ")
                query.Append(" FROM oPromotionCustomerExclusive T0 JOIN oReferenceValue T1 ON T0.SourceType = T1.ReferenceValue ")
                query.Append(" WHERE T1.ReferenceCode = 'CustomerSourceType' AND T0.PromotionID  =  @Param1")
                query.Append(" AND T1.ReferenceName ='Query String')")
                query.Append(" ) AS T0")
                query.Append(" ORDER BY LineID DESC")

                ds = connect.ObjectGetAll_Query_SAP(query.ToString(), New Object() {promotionID})
            End If
            Return ds
        Catch ex As Exception
            Throw ex
        End Try
    End Function
End Class
